﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;
#foreach($u in $TableInfos)
#beforeall 
#each 
#foreach($c in $u.ColumnInfos)  
#beforeall
#set($tmp1="") #set($tmp2="")#set($tmp3="")#set($tmpPK="")#set($tbCount=0)#set($tmpTypeName="")#set($tmpPK2="")
#each 
#if($c.IsPK=="True") #set($tmpPK="$c.ColumnName=@$c.ColumnName") #set($tmpPK2="$c.ColumnName") #set($tmpTypeName="$c.TypeCSharpName")#end
#if($tbCount==0)
#set($tmp1=$c.ColumnName)#set($tmp2="@$c.ColumnName")
#if($c.IsPK!="True")
#set($tmp3="$c.ColumnName=@$c.ColumnName")
#end
#else
#set($tmp1="$tmp1,$c.ColumnName") #set($tmp2="$tmp2,@$c.ColumnName")
#if($c.IsPK!="True")
#if($tmp3!="")
#set($tmp3="$tmp3,$c.ColumnName=@$c.ColumnName")
#else
#set($tmp3="$c.ColumnName=@$c.ColumnName")
#end
#end
#end
#if($c.IsPK!="True")
#set($tbCount=$tbCount+1)
#end
#afterall
#end
namespace DTcms.DAL
{
    /// <summary>
    /// $u.TabName ($u.TabCaption )
    /// </summary>
    public partial class ${u.TabName}
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ${tmpPK2})
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from $u.TabName");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@${tmpPK2}", SqlDbType.Int,10)
			};
            parameters[0].Value = ${tmpPK2};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DTcms.Model.${u.TabName} model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ${u.TabName}(");
            strSql.Append("$tmp1)");
            strSql.Append(" values (");
            strSql.Append("$tmp2)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
#foreach($c in $u.ColumnInfos)  
#beforeall
#set($tbCount=0)#set( $listCount=$u.ColumnInfos.Count)
#each
#set($tbCount=$tbCount+1)
#if($c.IsPK!="True")
#if($tbCount==$listCount)
                    new SqlParameter("@$c.ColumnName", SqlDbType.$c.TypeDbName,$c.Length)};
#else 
                    new SqlParameter("@$c.ColumnName", SqlDbType.$c.TypeDbName,$c.Length),
#end
#end
#end

#foreach($c in $u.ColumnInfos)  
#beforeall
#set($tbCount=0)#set( $listCount=$u.ColumnInfos.Count)
#each
#if($c.IsPK!="True")
            parameters[$tbCount].Value = model.$c.ColumnName;
#set($tbCount=$tbCount+1)
#end
#end
            
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DTcms.Model.${u.TabName} model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ${u.TabName} set ");
            strSql.Append("$tmp3");
            strSql.Append(" where $tmpPK");
            SqlParameter[] parameters = {
				#foreach($c in $u.ColumnInfos)  
#beforeall
#set($tbCount=0)#set( $listCount=$u.ColumnInfos.Count)
#each
#set($tbCount=$tbCount+1)
#if($tbCount==$listCount)
                    new SqlParameter("@$c.ColumnName", SqlDbType.$c.TypeDbName,$c.Length)};
#else 
                    new SqlParameter("@$c.ColumnName", SqlDbType.$c.TypeDbName,$c.Length),
#end
#end

#foreach($c in $u.ColumnInfos)  
#beforeall
#set($tbCount=0)#set( $listCount=$u.ColumnInfos.Count)
#each
            parameters[$tbCount].Value = model.$c.ColumnName;
#set($tbCount=$tbCount+1)
#end

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int $tmpPK2)
        {
            List<CommandInfo> sqllist = new List<CommandInfo>();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ${u.TabName} ");
            strSql.Append(" where $tmpPK");
            SqlParameter[] parameters = {
					new SqlParameter("@$tmpPK2", SqlDbType.Int,10)};
            parameters[0].Value = $tmpPK2;

            CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);
            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DTcms.Model.${u.TabName} GetModel(int $tmpPK2)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from ${u.TabName} ");
            strSql.Append(" where $tmpPK");
            SqlParameter[] parameters = {
					new SqlParameter("@$tmpPK2", SqlDbType.Int,10)
			};
            parameters[0].Value = $tmpPK2;

            DTcms.Model.${u.TabName} model = new DTcms.Model.${u.TabName}();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
#foreach($c in $u.ColumnInfos)  
#beforeall
#each
  			if (ds.Tables[0].Rows[0]["$c.ColumnName"]!=null && ds.Tables[0].Rows[0]["$c.ColumnName"].ToString()!="")  model.$c.ColumnName=#if($c.TypeCSharpName=="String")
ds.Tables[0].Rows[0]["$c.ColumnName"].ToString(); //$c.DeText
#else
 ${c.TypeCSharpName}.Parse(ds.Tables[0].Rows[0]["$c.ColumnName"].ToString()); //$c.DeText
#end
#afterall
#end
								ds.Dispose();
                return model;
            }
            else
            {		ds.Dispose();
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ${u.TabName} ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by $tmpPK2 desc");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM ${u.TabName} ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

 	/// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ${u.TabName}");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        #endregion  Method
    }
}
#end
